home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / LIB / MUI / TEXTLIST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  10.5 KB  |  439 lines

  1. /*
  2.  * Copyright (c) 1990,1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. #include <GL/glut.h>
  39. #include <ctype.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <mui/displaylist.h>
  43. #include <mui/uicolor.h>
  44. #include <mui/gizmo.h>
  45. #include <mui/textlist.h>
  46. #include <string.h>
  47.  
  48. extern  short sharefont1;
  49.  
  50. extern    TextList *locatedtl;
  51.  
  52. int    gettlheightcount(int ymin, int ymax)
  53. {
  54.     if (ymin > ymax) {
  55.     int tmp = ymin;
  56.     ymin     = ymax;
  57.     ymax     = tmp;
  58.     }
  59.     return ((ymax-ymin+1-7)/TEXTHEIGHT);
  60. }
  61.  
  62. void    movetl(TextList *tl, int ymin, int ymax)
  63. {
  64.     tl->listheight = gettlheightcount(ymin, ymax);
  65. }
  66.  
  67. int    gettextlistheight(int count)
  68. {
  69.     return (count*TEXTHEIGHT+7);
  70. }
  71.  
  72. TextList    *newtl(muiObject *obj, int listheight)
  73. {
  74.     TextList    *tl;
  75.  
  76.     tl = (TextList *)malloc(sizeof(TextList));
  77.  
  78.     tl->strs = 0;
  79.     tl->top = 0;
  80.     tl->selecteditem = -1;
  81.     tl->locateditem = -1;
  82.     tl->listheight = listheight;
  83.  
  84.     movetl(tl, obj->ymin, obj->ymax = obj->ymin+gettextlistheight(listheight));
  85.  
  86.     return tl;
  87. }
  88.  
  89. void    resettl(TextList *tl)
  90. {
  91.     tl->selecteditem = -1;
  92.     tl->top = 0;
  93. }
  94.  
  95. void    drawtl(muiObject *obj)
  96. {
  97.     TextList *tl = (TextList *)obj->object;
  98.     int    xmin = obj->xmin, xmax = obj->xmax, 
  99.         ymin = obj->ymin, ymax = obj->ymax;
  100.     int    item = 0, ybot, xright;
  101.     char    **s = &tl->strs[tl->top];
  102.  
  103.     if (!muiGetVisible(obj))
  104.     return;
  105.  
  106.     drawsetup();
  107.  
  108.     drawedges(xmin++, xmax--, ymin++, ymax--, uiMmGray, uiWhite);
  109.     drawedges(xmin++, xmax--, ymin++, ymax--, uiDkGray, uiVyLtGray);
  110.     drawedges(xmin++, xmax--, ymin++, ymax--, uiVyDkGray, uiDkGray);
  111.  
  112.     drawedges(xmin++, xmax--, ymin++, ymax--, uiVyLtGray, uiMmGray);
  113.  
  114.     uiLtGray();
  115.         uirectfi(xmin, ymin, xmax, ymax);
  116.  
  117.     uiBlack();
  118.     uipushviewport();
  119.     uiviewport(xmin+3, ymin-1, xmax-xmin-6, ymax-ymin);
  120.     if (s) while (*s && item < tl->listheight) {
  121.     if (item == tl->selecteditem - tl->top && muiGetEnable(obj)) {
  122.         ybot = ymin + 1 + (tl->listheight - item - 1)*TEXTHEIGHT;
  123.         xright = xmin + 11 + FONTWIDTH*(int)strlen(*s);
  124.             uiWhite();
  125.             uirectfi(xmin+4, ybot, xright, ybot + TEXTHEIGHT - 3);
  126.     }
  127.     
  128.     /* locate highlight */
  129.     if (item == tl->locateditem - tl->top && muiGetEnable(obj)) {
  130.         ybot = ymin + 1 + (tl->listheight - item - 1)*TEXTHEIGHT;
  131.             xright = xmin + 11 + FONTWIDTH*(int)strlen(*s);
  132.             uiWhite();
  133.                 uirecti(xmin+3, ybot-1, xright+1, ybot + TEXTHEIGHT - 2);
  134.                 uirecti(xmin+4, ybot, xright, ybot + TEXTHEIGHT - 3);
  135.     }
  136.         if (muiGetEnable(obj)) uiBlack(); else uiDkGray();
  137.         uicmov2i(xmin+8, ymin + 6 + (tl->listheight - item - 1)*TEXTHEIGHT);
  138.         uicharstr(*s, UI_FONT_NORMAL);
  139.     item++; s++;
  140.     }
  141.     uiBlack();
  142.     uipopviewport();
  143.     drawrestore();
  144. }
  145.  
  146. int    muiGetTLSelectedItem(muiObject *obj)
  147. {
  148.     TextList *tl = (TextList *)obj->object;
  149.     return tl->selecteditem;
  150. }
  151.  
  152. void    muiSetTLStrings(muiObject *obj, char **s)
  153. {
  154.     TextList *tl = (TextList *)obj->object;
  155.     tl->strs = s;
  156.  
  157.     tl->count = 0;
  158.     while(*s) {
  159.     tl->count++;
  160.     s++;
  161.     }
  162.     resettl(tl);
  163.     tl->selecteditem = 0;
  164. }
  165.  
  166. /* ARGSUSED3 */
  167. enum muiReturnValue tlhandler(muiObject *obj, int event, int value, int x, int y)
  168. {
  169.     int    i;
  170.     TextList *tl = (TextList *)obj->object;
  171.  
  172.     if( !muiGetEnable(obj) || !muiGetVisible(obj) ) return MUI_NO_ACTION;
  173.  
  174.     if (event == MUI_DEVICE_DOUBLE_CLICK) {
  175.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  176.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  177.     return MUI_TEXTLIST_RETURN_CONFIRM;
  178.     }
  179.     if (event == MUI_DEVICE_RELEASE) {
  180.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  181.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  182.     return MUI_TEXTLIST_RETURN;
  183.     }
  184.     if (event == MUI_DEVICE_PRESS || event == MUI_DEVICE_DOWN) {
  185.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  186.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  187.     return MUI_NO_ACTION;
  188.     }
  189.     return MUI_NO_ACTION;
  190. }
  191.  
  192. void    muiSetTLTopInt(muiObject *obj, int top)
  193. {
  194.     TextList *tl = (TextList *)obj->object;
  195.     if (top < 0) top = 0;
  196.     if (top >= tl->count) top = tl->count - 1;
  197.     tl->top = top;
  198. }
  199.  
  200. void    muiSetTLTop(muiObject *obj, float p)
  201. {
  202.     TextList *tl = (TextList *)obj->object;
  203.     if (tl->count <= tl->listheight) { tl->top = 0; return; }
  204.     tl->top = (1.0-p)*(tl->count + 1 - tl->listheight);
  205. }
  206.  
  207.  
  208. void    settlstrings(TextList *tl, char **s)
  209. {
  210.     tl->strs = s;
  211.  
  212.     tl->count = 0;
  213.     while(*s) {
  214.         tl->count++;
  215.         s++;
  216.     }
  217. }
  218.  
  219. #ifdef NOTDEF
  220.  
  221. /* SELECT STATE FUNCTIONS */
  222.  
  223. void    unselecttl(TextList *tl)
  224. {
  225.     int oldselect = tl->selecteditem;
  226.  
  227.     if (tl->selecteditem == -1)
  228.     return;
  229.     tl->selecteditem = -1;
  230.     if (!tl->invisible)
  231.     if (oldselect != -1)
  232.         drawparttl(tl,oldselect);
  233. }
  234.  
  235. void    selecttlitem(TextList *tl, int item)
  236. {
  237.     int oldselect = tl->selecteditem;
  238.  
  239.     if (tl->selecteditem == item)
  240.     return;
  241.     tl->selecteditem = item;
  242.     if (!tl->invisible) {
  243.     if (tl->selecteditem != -1)
  244.         drawparttl(tl,tl->selecteditem);
  245.     if (oldselect != -1)
  246.         drawparttl(tl,oldselect);
  247.     }
  248. }
  249.  
  250. short    getselectedtlitem(TextList *tl)
  251. {
  252.     return tl->selecteditem;        /* returns -1 if none selected */
  253. }
  254.  
  255. void    highlighttl(TextList *tl, int item)
  256. {
  257.     int oldhighlight = tl->locateditem;
  258.  
  259.     if (tl->locateditem == item)
  260.     return;
  261.     tl->locateditem = item;
  262.     locatedtl = tl;
  263.     if (!tl->invisible) {
  264.     if (tl->locateditem != -1)
  265.         drawparttl(tl,tl->locateditem);
  266.     if (oldhighlight != -1)
  267.             drawparttl(tl,oldhighlight);
  268.     }
  269. }
  270.  
  271. void    unhighlighttl(TextList *tl)
  272. {
  273.     int oldhighlight = tl->locateditem;
  274.  
  275.     if (tl->locateditem == -1)
  276.     return;
  277.     tl->locateditem = -1;
  278.     if (locatedtl == tl)
  279.     locatedtl = 0;
  280.     if (!tl->invisible) {
  281.         if (oldhighlight != -1)
  282.             drawparttl(tl,oldhighlight);
  283.     }
  284. }
  285.  
  286. short    gethighlighttl(TextList *tl)
  287. {
  288.     return tl->locateditem;
  289. }
  290.  
  291. void    makevisibletl(TextList *tl)
  292. {
  293.     if (!tl->invisible)
  294.     return;
  295.     tl->invisible = 0;
  296. }
  297.  
  298. void    makeinvisibletl(TextList *tl)
  299. {
  300.     if (tl->invisible)
  301.     return;
  302.     tl->invisible = 1;
  303. }
  304.  
  305. /* whether or not the text list is showing */
  306. short   getvisibletl(TextList *tl)
  307. {
  308.     return 1-tl->invisible;
  309. }
  310.  
  311. /* whether or not the item is showing */
  312. short    getitemvisibletl(TextList *tl, int item)
  313. {
  314.     if ((item < tl->top) || (item >= (tl->top+tl->listheight)) || 
  315.     (item >= tl->count) || (item == -1))
  316.     return 0;
  317.     else
  318.     return 1;
  319. }
  320.  
  321. short    locatetl(TextList *tl, int x, int y)
  322. {
  323.     int highlight = gethighlighttl(tl);
  324.     int intextlist  = intl(tl,x,y);
  325.  
  326.     if ((intextlist == highlight) || !getvisibletl(tl))
  327.     return 0;
  328.     if (intextlist != -1) {
  329.     unlocateall();
  330.         highlighttl(tl,intextlist);
  331.         return 1;
  332.     } else {
  333.     unhighlighttl(tl);
  334.     return 0;
  335.     }
  336. }
  337.  
  338. char *selectedstring(TextList *tl)
  339. {
  340.     if (tl->selecteditem == -1) return 0;
  341.     return tl->strs[tl->selecteditem];
  342. }
  343.  
  344. void    drawtlnow(TextList *tl)
  345. {
  346.     uifrontbuffer(1);
  347.     drawtl(tl);
  348.     uifrontbuffer(0);
  349. }
  350.  
  351. void    drawparttl(TextList *tl,int item)
  352. {
  353.     int    xmin = tl->xmin+8, xmax = tl->xmax-8-SLIDERWIDTH;
  354.     int    ymin = 
  355.         tl->ymin+5+(tl->listheight - item + tl->top - 1)*TEXTHEIGHT;
  356.     int     ymax = ymin + TEXTHEIGHT - 3;
  357.     char        **s;
  358.  
  359.     if (!getvisibletl(tl) || !getitemvisibletl(tl,item))
  360.     return;
  361.  
  362.     s = &tl->strs[item];
  363.     drawsetup();
  364.  
  365.     uifrontbuffer(1);
  366.  
  367.     uiLtGray();
  368.         uirectfi(xmin-1, ymin-1, xmax+1, ymax+1);
  369.  
  370.     font(TEXTLISTFONT);
  371.     pushviewport();
  372.     scrmask(xmin-1, xmax+1, ymin-1, ymax+1);
  373.     if (s) {
  374.         xmax = xmin + 7 + strwidth(*s);
  375.         if (item == tl->selecteditem) {
  376.             uiWhite();
  377.                 uirectfi(xmin, ymin, xmax, ymax);
  378.         }
  379.         if (item == tl->locateditem) {
  380.             uiWhite();
  381.                 uirecti(xmin-1, ymin-1, xmax+1, ymax+1);
  382.                 uirecti(xmin, ymin, xmax, ymax);
  383.         }
  384.         uiBlack();
  385.             uicmov2i(xmin+4, ymin + 5);
  386.             uicharstr(*s);
  387.     }
  388.     popviewport();
  389.  
  390.     uifrontbuffer(0);
  391.  
  392.     drawrestore();
  393. }
  394.  
  395. void    settlstrslinkhandle(char **l, TextList *tl)
  396. {
  397.     int    vshalf, d;
  398.     float    p;
  399.  
  400.     settlstrings(tl, l);
  401.  
  402.     if (tl->count <= tl->listheight) {
  403.     vshalf = ((tl->listheight*TEXTHEIGHT - 36)/2) - 2;
  404.     d = 0;
  405.     p = 1.0;
  406.     disablevs(tl->vs);
  407.     } else {
  408.     enablevs(tl->vs);
  409.     p = getvsval(tl->vs);
  410.     vshalf = tl->listheight*(tl->listheight*TEXTHEIGHT-36)/(2*tl->count);
  411.     d = (tl->listheight*TEXTHEIGHT - 2*vshalf)/(tl->count - tl->listheight);
  412.     if (d == 0) d = 1;
  413.     }
  414.     setvsarrowdelta(tl->vs, d);
  415.     setvshalf(tl->vs, vshalf);
  416.     movevsval(tl->vs, p);
  417. }
  418.  
  419. void    adjustslider(TextList *tl, VSlider *sl)
  420. {
  421.     setvsval(sl, (float) (1.0 - (float) tl->top/ (float) tl->count));
  422. }
  423.  
  424. short    selectedtl(TextList *tl, int x, int y, int val)
  425. {
  426.     if (intl(tl, x, y) == -1) {
  427.     if (intlboundaries(tl, x, y)) {
  428.             tl->selecteditem = -1;
  429.         drawtlnow(tl);
  430.     }
  431.     } else {
  432.         if (val == UIBUTTONUP) return 0;
  433.         return handletl(tl, LEFTMOUSE, val);
  434.     }
  435.     return 0;
  436. }
  437.  
  438. #endif /* NOTDEF */
  439.